/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package descriptor;

import exceptions.DatabaseException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import presentation.LinkElement;
import presentation.PresentationElement;
import presentation.PresentationElementFactory;
import runtime.RuntimeInstanceFactory;

/**
 *
 * @author Alejandro
 */
public class Entity implements EntityPresentation {
    private String tableName;
    private Iterable<Property> properties;
    private Collection<PresentationElement> presentations;

    public Entity(String tableName) {
        this.tableName = tableName;
        properties = new LinkedList<Property>();
        presentations = new LinkedList<PresentationElement>();
    }

    public Property getProperty(String name) {
        Property result = null;
        for (Property p: properties) {
            if (p.getName().equals(name)) {
                result = p;
                break;
            }
        }
        return result;
    }

    public Iterable<Property> getPropertyCollection() {
        return properties;
    }

    public PresentationElement getPresentationElement(String alias) {
        PresentationElement result = null;
        for (PresentationElement pe: presentations) {
            if (pe.getAlias().equals(alias)) {
                result = pe;
                break;
            }
        }
        return result;
    }

    static void sort(List<Entity> container) {
        Collections.sort(container, new EntityComparator());
    }

    public void parse(Initializer i) {
        properties = (Iterable<Property>) i.init(tableName);
        Application app = Application.getInstance();
        Template template = app.getTemplate(tableName);
        Iterable<PresentationTemplate> container = template.getPresentationTemplateCollection();
        for (PresentationTemplate pt: container) {
            RuntimeInstanceFactory factory = new PresentationElementFactory();
            PresentationElement pe = (PresentationElement) factory.getObject(pt.getTypeName());
            pe.setName(pt.getName());
            pe.setAlias(pt.getAlias());
            String value = getProperty(pt.getProperty()).getValue().toString();
            PropertyTemplate pTemplate = template.getPropertyTemplate(pt.getProperty());
            if (pTemplate.isNotNull() || !value.equals(pTemplate.getNullReplacingValue())) {
                pe.setValue(value);
                Iterator<External> it2 = pt.getExternals().iterator();
                int j = 0;
                while (it2.hasNext()) {
                    try {
                        External ext = it2.next();
                        EntityDAO dao = new EntityDAO(ext.getTable());
                        Entity e2 = (Entity) dao.findByPrimaryKey(pe.getValue());
                        PresentationElement p2 = e2.getPresentationElement(ext.getPresentation());
                        List<Object> externalMap = new LinkedList<Object>();
                        externalMap.add(p2);
                        externalMap.add(ext.isReplacing());
                        pe.setParam(LinkElement.ADD_EXTERNAL, externalMap);
                    } catch (DatabaseException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            } else {
                pe.setValue(pt.getNullReplacingValue());
            }
            presentations.add(pe);
        }
    }

    @Override
    public Iterable<PresentationElement> getPresentationCollection() {
        return (Iterable<PresentationElement>) presentations;
    }

    @Override
    public int compareTo(EntityPresentation o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
